package JDBC.jdbc;

import JDBC.util.JDBCUtil;

import java.sql.*;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.Scanner;

public class JDBCTest {
    public static void main(String[] args) {
//
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();

        System.out.println("请输入密码");
        String password = sc.nextLine();

        boolean login=login(username,password);
        if (login){
            System.out.println("登陆成功");
        }else {
            System.out.println("用户名或密码错误");
        }
    }

//          登录方法
    private static boolean login(String username, String password) {
        if (username==null||password==null){
            return false;
        }
//        连接数据库
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;
//        1.获取连接
        try{
            connection= JDBCUtil.GetConnection();
//            2.定义sql

String sql="select * from account where username='"+username+"' and password='"+password+"'";

//            3.获取sql执行的对象
            statement=connection.createStatement();
//            4.执行查询
            resultSet=statement.executeQuery(sql);

//            判断
            return resultSet.next();//如果有下一行，则返回true
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtil.close(connection,statement,resultSet);

        }
        return false;
    }
}
